package com.aizhixin.lab.docker.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.docker.common.AuthUtilService;
import com.aizhixin.lab.docker.common.ExceptionAuth;
import com.aizhixin.lab.docker.common.ExceptionValidation;
import com.aizhixin.lab.docker.common.ReturnJsonData;
import com.aizhixin.lab.docker.domain.ChapterImagesDomain;
import com.aizhixin.lab.docker.domain.ChapterImagesDomainV2;
import com.aizhixin.lab.docker.entity.ChapterImages;
import com.aizhixin.lab.docker.service.ChapterImagesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/docker/chapter")
@Api(value = "docker章节镜像api", description = "docker章节镜像api")
public class ChapterImagesController {
    @Autowired
    private AccountService authUtilService;
    @Autowired
    private ChapterImagesService chapterImagesService;

    @RequestMapping(value = "/vm/save",method = RequestMethod.POST)
    @ApiOperation(value = "设定章节镜像环境",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ReturnJsonData<String> save(@RequestHeader("Authorization")String token, @RequestBody ChapterImagesDomain chapterImagesDomain){
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null){
            throw new ExceptionAuth(401,"无权限");
        }
        chapterImagesDomain.setUserId(accountDTO.getId());
        ChapterImages chapterImages = chapterImagesService.saveChapterImages(chapterImagesDomain);
        ReturnJsonData<String> returnJsonData = new ReturnJsonData<>();
        returnJsonData.setData(chapterImages.getId());
        return returnJsonData;
    }

    @RequestMapping(value = "/vm/get/{chapterId}",method = RequestMethod.GET)
    @ApiOperation(value = "获取章节镜像环境",httpMethod = "GET",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ReturnJsonData<ChapterImagesDomainV2> get(@RequestHeader("Authorization")String token, @ApiParam(value = "chapterId章节id") @PathVariable("chapterId")Long chapterId){
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null){
            throw new ExceptionAuth(401,"无权限");
        }
        if (StringUtils.isEmpty(chapterId)){
            throw new ExceptionValidation(417,"章节id不能为空");
        }
        ChapterImagesDomainV2 chapterImagesDomainV2List = chapterImagesService.findChapterId(chapterId);
        ReturnJsonData<ChapterImagesDomainV2> returnJsonData = new ReturnJsonData<>();
        returnJsonData.setData(chapterImagesDomainV2List);
        return returnJsonData;
    }


}
